##################################################
# Deploy.ps1
# Prototype for Deployment
# 
# Author: cash.foley@ps.net
##################################################

param( [string] $TargetEnv = "Unknown"
     , [string] $TargetServer = "all"
     , [string] $TargetApp = "all"
     , [switch] $Help
     , [switch] $StopServicesOnly
     , [switch] $StartServicesOnly
     , [switch] $Verbose
     , [switch] $Confirm
     , [switch] $Execute
     , [switch] $NoPrompt
		 , [switch] $OnTimeReport
		 , [switch] $emailNotification
		 , [switch] $OnTimeUpdate
		 , [switch] $Notifications
		 , [switch] $BizTalkPrimary
     )
     
## Biztalk deployment Primary/Secondary control     
$GLOBAL:BizTalk_primary = $BizTalkPrimary
$currentLocation = get-location 

$NoPromptSave = $NoPrompt

#################################################################################

$Whatif = !$Execute

. ((get-childitem -recurse -filter PowerShellInit.ps1).fullname)

. Init-Scripts (join-path (Get-ScriptDir) "Install\DeployFunctions")



#################################################################################
# MAIN
#################################################################################
$NoPrompt = $NoPromptSave

#$CommonProductParameters = ReadConfigFile (join-path (get-location) "Install/CommonProductParameters.xml")

$Global:custApps = @()
$Global:dbDeployments = @()

$AllEnv = ReadDeploymentConfig (join-path (get-location) "Install/DeploymentEnv.xml")

##### Collect Properties for notifications ######
$_dir = Get-ScriptDir
[xml]$Deploymentinfo = get-content "$_dir\Install\CommonProductParameters.xml"
$productname = $Deploymentinfo.variables.ProjectDashboardProductName
$Version = $Deploymentinfo.variables.ProductVersion
$Pattern="(?<Major>\d*)\.(?<Minor>\d*)\.(?<Build>\d*)\.(?<revision>\d*)"
$null = $Version -match $Pattern
$MajorVersion = $matches.Major
$MinorVersion = $matches.Minor
[string]$OnTimeEnvCode = $AllEnv | ?{$_.name -eq $TargetEnv} | %{$_.OnTimeEnvCode}

### Begin Log
if ($LogFileName -eq $null)
{
  $thisLog = "{1}-{0:MM.dd.yy.HH.mm.ss}.log" -f  [datetime]::Now, "$($TargetEnv.ToUpper()).v$($Version)"
  $thisLog = Log-InititialzieFile $thisLog
}

if ($TargetEnv -eq "Unknown")
{
  Log-Message "Please specify a valid target environment. Refer to deploymentenv.xml" -foregroundcolor yellow
  Log-Message "Example: ./deploy -TargetEnv dev" -foregroundcolor yellow
  exit
}

Perform-Deployment 

trap {
      Log-Error "Error while executing Deployment"
      Log-Error $_
      return (-1)
}

#Send OnTime Update
if ($OnTimeUpdate -or $Notifications)
{
	$msg = "Update OnTime of Deployment Event for $productname version $version to $TargetEnv code $OnTimeEnvCode"
	if (Should-Process $msg ([REF]$AllAnswer) "" -Verbose:$Verbose -Confirm:$Confirm -Whatif:$Whatif)
	{
	  if (-not [system.String]::IsNullOrEmpty($OnTimeEnvCode))
		{
		  ReTry "Failure - Update Ontime Deployment Event"{ OnTime-Update -ProductName $productname -BuildNumber $version -EnvironmentCode $OnTimeEnvCode } -AllowSkip 
		}
	}
}

if ($OnTimeReport -or $Notifications)
{
	$msg = "Generating OnTime Report"
	if (Should-Process $msg ([REF]$AllAnswer) "" -Verbose:$Verbose -Confirm:$Confirm -Whatif:$Whatif)
	{
		if (-not [system.String]::IsNullOrEmpty($OnTimeEnvCode))
		{
		  ReTry "Generating OnTime Report"{ OnTime-Report $productname $MajorVersion $MinorVersion $OnTimeEnvCode $TargetEnv $Version } -AllowSkip 
		}
	}
}
if ($logFileName -eq $thisLog)
{
    Log-StopFile
}
#OnTime-Report

if ($emailNotification -or $Notifications)
{
	$msg = "Send Email Notification"
	if (Should-Process $msg ([REF]$AllAnswer) "" -Verbose:$Verbose -Confirm:$Confirm -Whatif:$Whatif)
	{
		SendDeploymentMail $Global:OntimeReportFileDest
	}
}

#Write-Host "Unmap drive shares if desired by entering the following cmd Remove-NonDriveShares."
######################################################################################
